﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using homework5;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace homework12.Controllers
{
    [ApiController]
    [Route("api/Order")]
    public class OrderController : ControllerBase
    {
    private readonly OrderContext OrderDb;
        private readonly OrderService os;

        OrderController(OrderContext context, OrderService os)
        {
            this.OrderDb = context;
            this.os = os;
        }

        // 按订单号查询订单
        [HttpGet("SearchById/{id}")]
        public ActionResult<Order> SearchById(int id)
        {
            Order o = os.SearchByNum(id);
            if (o == null)
            {
                return NotFound();
            }
            return o;
        }

        // 按顾客姓名查询订单
        [HttpGet("SearchByCustomer/{name}")]
        public ActionResult<Order> SearchByCustomer(string name)
        {
            Order o = os.SearchByCustomer(name);
            if (o == null)
            {
                return NotFound();
            }
            return o;
        }

        // 按订单金额查询订单
        [HttpGet("SearchByMoney/{money}")]
        public ActionResult<Order> SearchByMoney(double money)
        {
            Order o = os.SearchByMoney(money);
            if (o == null)
            {
                return NotFound();
            }
            return o;
        }

        // 添加订单
        [HttpPost("Add")]
        public ActionResult<Order> PostOrder(Order order)
        {
            try
            {
                OrderDb.Orders.Add(order);
                OrderDb.SaveChanges();
            }
            catch (Exception ex)
            {
                return BadRequest(ex.InnerException.Message);
            }
            return order;
        }

        // 修改订单顾客
        [HttpPut("Update/{id}")]
        public ActionResult<Order> PutOrder(int id, Order order)
        {
            if (id != order.OrderId)
            {
                return BadRequest("Id cannot be modified!");
            }
            try
            {
                OrderDb.Entry(order).State = EntityState.Modified;
                OrderDb.SaveChanges();
            }
            catch (Exception ex)
            {
                string error = ex.Message;
                if (ex.InnerException != null)
                    error = ex.InnerException.Message;
                return BadRequest(error);
            }
            return NoContent();
        }

        // 删除订单
        [HttpDelete("delete/{id}")]
        public ActionResult<Order> Delete(int id)
        {
            try
            {
                var order = OrderDb.Orders.FirstOrDefault(o => o.OrderId == id);
                if (order != null)
                {
                    OrderDb.Remove(order);
                    OrderDb.SaveChanges();
                }
            }catch(Exception ex)
            {
                return BadRequest(ex.InnerException.Message);
            }
            return NoContent();
        }
    }
}
